package com.zzx.service.impl;

import com.zzx.utils.EntityUtils;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.common.exception.ServiceException;
import com.zzx.entity.TProductOrderDept;
import com.zzx.mapper.TProductOrderDeptMapper;
import com.zzx.service.TProductOrderDeptService;
import com.zzx.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author zzx
 * @description 针对表【t_product_order_dept(生产单-下发部门表)】的数据库操作Service实现
 * @createDate 2025-07-13
 */
@Service
public class TProductOrderDeptServiceImpl extends ServiceImpl<TProductOrderDeptMapper, TProductOrderDept>
        implements TProductOrderDeptService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TProductOrderDept param) {
        LambdaQueryWrapper<TProductOrderDept> wrapper = new LambdaQueryWrapper<>(TProductOrderDept.class);
        // 多租户拦截器会自动添加 company_id 条件

        if (ObjectUtils.isEmpty(param.getProductOrderDeptId())) {
            param.setProductOrderDeptId(IdUtil.getSnowflakeNextId());
        } else {
            wrapper.ne(TProductOrderDept::getProductOrderDeptId, param.getProductOrderDeptId());
            TProductOrderDept dbEntity = this.getBaseMapper().selectById(param.getProductOrderDeptId());
            if (dbEntity != null) {
                EntityUtils.mergeNonNullProperties(dbEntity, param);
            }
        }

        List<TProductOrderDept> existList = this.getBaseMapper().selectList(wrapper);
        if (!existList.isEmpty()) {
            throw new ServiceException("部门订单已存在");
        }

        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TProductOrderDept param) {
        LambdaQueryWrapper<TProductOrderDept> wrapper = new LambdaQueryWrapper<>(TProductOrderDept.class);
        // 多租户拦截器会自动添加 company_id 条件
        if (!ObjectUtils.isEmpty(param.getProductOrderId())) {
            wrapper.eq(TProductOrderDept::getProductOrderDeptId, param.getProductOrderId());
        }
        if (!ObjectUtils.isEmpty(param.getProductOrderDeptId())) {
            wrapper.eq(TProductOrderDept::getProductOrderDeptId, param.getProductOrderDeptId());
        }
       this.remove(wrapper);
    }

    @Override
    public IPage<TProductOrderDept> selectBy(TProductOrderDept param) {
        LambdaQueryWrapper<TProductOrderDept> wrapper = new LambdaQueryWrapper<>(TProductOrderDept.class);


        if (!ObjectUtils.isEmpty(param.getProductOrderDeptId())) {
            wrapper.eq(TProductOrderDept::getProductOrderDeptId, param.getProductOrderDeptId());
        }

        // 多租户拦截器会自动添加 company_id 条件
        wrapper.orderByDesc(TProductOrderDept::getCreateTime);

        return this.getBaseMapper().selectPage(
                PageUtils.createPage(param.getPageNum(), param.getPageSize()),
                wrapper
        );
    }

    @Override
    public TProductOrderDept findById(String id) {
        return this.getBaseMapper().selectById(id);
    }
}
